$(function() {
    $('#document-file').change(function () {
		uploadFile();
	});
});



function uploadDocument(obj) {

    now_type = $(obj).attr('data-type');
    $('#document-file').click();
}


function bannerPrev(obj) {
	var can_prev = $(obj).parents('.data-box').prev().hasClass('data-box-banner');
	
	if (can_prev) {
		$(obj).parents('.data-box').prev().before($(obj).parents('.data-box'));
	}
}

function bannerNext(obj) {
	var can_next = $(obj).parents('.data-box').next().hasClass('data-box-banner');
	if (can_next) {
		$(obj).parents('.data-box').next().after($(obj).parents('.data-box'));
	}
}


function uploadFile() {

    var fileObj = document.getElementById("document-file").files; // js 获取文件对象
    if (fileObj.length == 0) {
        errorAlert('请选择图片');
        return;
    }
    var formFile = new FormData();
    for (var i = 0; i < fileObj.length; i++) {
        formFile.append("myfile[]", fileObj[i]); //加入文件对象
    }
    $('.document-p .progress-bar').css('width', '0%');
    $('.document-p .p-text').html('已上传（0%）');
    $('.document-p').show();
    var data = formFile;
    setTimeout(function () {
        $.ajax({
            url:  getRootPath() +'/Admin/CommonUpload/upload/' + now_type,
            data: data,
            type: "POST",
            dataType: "json",
            cache: false, //上传文件无需缓存
            processData: false, //用于对data参数进行序列化处理 这里必须false
            contentType: false, //必须
            xhr: xhrOnProgress(function (e) {
                var percent = e.loaded / e.total;
                var now_jindu = (parseFloat(percent) * 100).toFixed(2);
                console.log(now_jindu);
                $('.document-p .progress-bar').css('width', now_jindu + '%');
                $('.document-p .p-text').html('已上传（' + now_jindu + '%）');
                $('.document-p').show();
            }),
            success: function (data) {
                $('.document-p .progress-bar').css('width', '100%');
                $('.document-p .p-text').html('已上传（100%）');
                $('.document-p').hide();
                if (data.status) {
                    // alert(now_type);
                    var path = JSON.parse(data.v);
                    var icon = JSON.parse(data.remark);       
                    $.each(path, function (k, v) {

						console.log(getRootPath() + '/' + v);
                        $('.data-box-demo img').attr('src', getRootPath() + '/' + v);
                        $('.data-box-demo a').attr('href', getRootPath() + '/' + v);
                        $('.data-box-demo .path').val(v).attr('name', 'SupervisionOtherImg');
                        //  $('.data-box-demo img').attr('src', getRootPath() + '/' + v);
                        //  $('.data-box-demo a').attr('href', getRootPath() + '/' +v);
                        // $('.data-box-demo .path').val(getRootPath() + '/' +v).attr('name', 'attend_img[' + now_type + '][]');
                        //  $('.data-box-demo .icon').val(icon[k]).attr('name', 'icon[' + now_type + '][]');
						console.log($('.data-box-demo').html());
                        $('#editForm .td_' + now_type).append($('.data-box-demo').html());
                    });
                } else {
                    window.parent.topMessagePrompt('error', data.msg);
                }
            }
        })
    }, 1000);
}